/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.carpark.rest;

import cz.muni.fi.pa165.carpark.EmployeePosition;
import cz.muni.fi.pa165.carpark.TO.EmployeeTO;
import cz.muni.fi.pa165.carpark.servicies.EmployeeService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Martin
 */
@Controller
@RequestMapping("/employee")
public class EmployeeRestService {

    final static Logger log = LoggerFactory.getLogger(EmployeeRestService.class);
    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public @ResponseBody Boolean delete(@PathVariable("id") Long id) {
        EmployeeTO emp = employeeService.retrieveEmp(id);
        try {
            employeeService.deleteEmp(emp);
        } catch (Exception e) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public @ResponseBody
    EmployeeTO get(@PathVariable("id") Long id) {
        return employeeService.retrieveEmp(id);
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public @ResponseBody
    void create(@RequestBody EmployeeTO emp, HttpServletRequest req, HttpServletResponse resp) {
        employeeService.createEmp(emp);

        resp.setStatus(HttpStatus.CREATED.value());
        resp.setHeader("Location", req.getContextPath() + "/rest/employee/" + emp.getId());

    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public void update(@PathVariable("id") Long id, @RequestBody EmployeeTO emp, HttpServletResponse resp) {

        employeeService.updateEmp(emp);

        resp.setStatus(HttpStatus.OK.value());

    }

    @RequestMapping(value = "/findall", method = RequestMethod.GET)
    public @ResponseBody
    List<EmployeeTO> find() {
        if (employeeService == null) {
            throw new IllegalArgumentException("SERVICE IS NULL");
        }
        return employeeService.retrieveAll();
    }

    @RequestMapping(value = "/findbycategory", method = RequestMethod.POST)
    public @ResponseBody
    List<EmployeeTO> findByCategory(@RequestBody EmployeePosition position) {
        return employeeService.retrieveByCategory(position);
    }
}
